package com.edu.ocrdemo.scheduled;

import com.edu.ocrdemo.entity.Task;
import com.edu.ocrdemo.repository.TaskRepository;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author tfan
 * @Description
 * @Date 2024/12/31 18:11
 **/
@Component
public class TaskCleaner {

    private final TaskRepository taskRepository;

    public TaskCleaner(TaskRepository taskRepository) {
        this.taskRepository = taskRepository;
    }

    /**
     * 定时清理已完成任务，释放内存资源
     */
    @Scheduled(fixedRateString = "${task.cleanup-interval}") //
    public void cleanUpCompletedTasks() {
        List<Task> completedTasks = taskRepository.getCompletedTasks();
        for (Task task : completedTasks) {
            taskRepository.delete(task.getTaskId());
            System.out.println("Task " + task.getTaskId() + " cleaned up.");
        }
    }
}
